/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.io.*;
import javax.swing.*;
import javax.swing.table.TableModel;

/**
 *
 * @author Antonio
 */
public class Excelexporter {
    
    Excelexporter(){}
    
    public void exportTableExcel(JTable table, File file) throws IOException{

        TableModel model = table.getModel();
        FileWriter out = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(out);
        
        for (int i=0; i< model.getColumnCount();i++){
            bw.write(model.getColumnName(i)+"\t");
        }
        bw.write("\n");

        for (int i=0; i<model.getRowCount();i++){
            for (int j=0; j<model.getColumnCount();j++){
                bw.write(model.getValueAt(i, j).toString()+"\t");
            }
            bw.write("\n");
        }
        bw.close();
        out.close();
        openExcel();
    }
    
    private void openExcel(){
        String file = "salida.xls";
        try
        {
           Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+file);
        }
        catch (Exception e)
        {
           System.out.println("Error al abrir el archivo " + file + "\n" + e.getMessage());
        }
    }
            
    
}
